﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace NCKH.Admin
{
    public partial class nhomtin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txttimkiem.Attributes.Add("PlaceHolder", "Nhập tên nhóm tin cần Tìm...");
            if (!IsPostBack)
                hienchuyenmuc();


        }

        private void hienchuyenmuc()
        {
            using (DataTable tbl = getchuyenmuc())
            {
                grvnhomtin.DataSource = tbl;
                grvnhomtin.DataBind();
            }
        }

        private DataTable getchuyenmuc()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string select = "SELECT Manhomtin, Tennhomtin FROM NHOMTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(select, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable tbl = new DataTable("NHOMTIN");
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }

        protected bool kiemtraten()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string ktten = "SELECT Tennhomtin From NHOMTIN WHERE Tennhomtin=N'" + txttennhomtin.Text + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(ktten, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            lbtb.Text = "Tên nhóm tin đã bị trùng";
                            // Response.Write("<script>alert('Tên đã bị trùng')</script>");
                            return true;
                        }
                        else
                        {
                            lbtb.Text = "";
                            return false;
                        }
                        //rd.Close();
                    }

                }
                cnn.Close();
            }
        }


        protected void btnthem_Click(object sender, EventArgs e)
        {
            if (kiemtraten() == false)
                themnhomtin();
        }


        protected void themnhomtin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand("themnhomtin", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Tennhomtin", txttennhomtin.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                    txttennhomtin.Text = string.Empty;
                    txttennhomtin.Focus();
                    hienchuyenmuc();
                }
            }

        }


        protected void btnhuy_Click(object sender, EventArgs e)
        {
            txttennhomtin.Text = "";
        }

        protected void btntimkiem_Click(object sender, EventArgs e)
        {
            hientimkiem();

        }

        protected void hientimkiem()
        {
            using (DataTable tb = gettimkiem())
            {
                grvnhomtin.DataSource = tb;
                grvnhomtin.DataBind();

            }
        }
        protected DataTable gettimkiem()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand("timkiemtheotennhomtin", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Tennhomtin", '%' + txttimkiem.Text + '%');
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        return tbl;
                    }

                }
            }
        }
        protected void grvnhomtin_DataBound(object sender, EventArgs e)
        {

        }

        protected void grvnhomtin_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int id = Convert.ToInt32(e.CommandArgument);
                string connection = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                string delete_nhomtin = "DELETE FROM NHOMTIN WHERE Manhomtin='" + id + "'";
                using (SqlConnection cnn = new SqlConnection(connection))
                {
                    using (SqlCommand cmd = new SqlCommand(delete_nhomtin, cnn))
                    {
                        cmd.CommandType = CommandType.Text;
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        cnn.Close();
                        hienchuyenmuc();
                    }

                }
            }
        }

        protected void grvnhomtin_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvnhomtin.PageIndex = e.NewPageIndex;
            hienchuyenmuc();
        }

        protected void lbtxoa_Command(object sender, CommandEventArgs e)
        {

        }

        protected void grvnhomtin_Sorting(object sender, GridViewSortEventArgs e)
        {


        }
    }
}